package com.ny.view;
import com.ny.aa.OrderService;
import com.ny.aa.ProductService;
import com.ny.aa.impl.OrderServiceImpl;
import com.ny.aa.impl.ProductServiceImpl;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.aa.StudentService;
import com.ny.aa.impl.StudentServiceImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class ViewMain {
    ProductService productService = new ProductServiceImpl();
    OrderService orderService = new OrderServiceImpl();
    StudentService studentService = new StudentServiceImpl();
    public void InterView() throws SQLException, ClassNotFoundException {
    }

    public  void indexview(Stud stud) throws SQLException, ClassNotFoundException {
        boolean f = true;
        while (f) {
            System.out.println("请选择你的操作");
            System.out.println("1.个人信息内容界面");
            System.out.println("2.商品信息界面");
            System.out.println("3.订单界面");
            System.out.println("4.修改密码");
            System.out.println("5.退出");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("1.个人信息内容界面");
                    System.out.println("学生id：" + stud.getStuID());
                    System.out.println("用户名：" + stud.getStuName());
                    System.out.println("手机号：" + stud.getStuPhone());
                    System.out.println("密码：" + stud.getStuPwd());
                    System.out.println("1.修改密码");
                    System.out.println("2.修改手机号码");
                    int choice2 = scanner.nextInt();
                    switch (choice2){
                        case 1:
                            String name = stud.getStuName();
                            System.out.println("请输入旧密码");
                            String pwd = scanner.next();
                            if (pwd.equals(stud.getStuPwd())) {
                                System.out.println("请输入新密码");
                                String newpwd = scanner.next();


                                Stud stud1 = studentService.changePwd(name, pwd);
                                if (stud1 != null) {
                                    stud1.setStuPwd(newpwd);
                                    studentService.changePwd(name, newpwd);
                                    System.out.println("修改成功");
                                } else {
                                    System.out.println("修改失败");

                                }
                            }else {
                                System.out.println("旧密码错误,无法修改");
                            }
                            break;
                        case 2:
                            System.out.println("请输入旧手机号");
                            String phone = scanner.next();
                            if (phone.equals(stud.getStuPhone())) {
                                System.out.println("请输入新手机号");
                                String newphone = scanner.next();

                            }
                            break;
                    }
                    break;
                case 2:
                    System.out.println("2.商品信息界面");
                    List<Product> list = productService.findAllProduct();
                    System.out.println("商品id" + "\t" + "\t" + "商品名称" + "\t" + "商品价格" + "\t" + "商品库存" + "\t" + "商品类别" + "\t" + "商品描述");
                    for (Product product : list) {
                        System.out.print(product.getProduct_id());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getName());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getPrice());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getStock());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getCategory());
                        System.out.print("\t" + "\t");
                        System.out.println(product.getDescription());

                    }
                    System.out.println("输入购买商品序号");
                    int choice1 = scanner.nextInt();
                    System.out.println("输入购买数量");
                    int num = scanner.nextInt();
                    //如果购买数量大于库存数量，则提示购买失败，否则购买成功，并编辑数据库的库存数量
                    int stuid = stud.getStuID();

                    double total_price = productService.findAllProduct().get(choice1-1).getPrice() * num;;
                    Order order = new Order();
                    order.setStudent_id(stuid);
                    order.setProduct_id(choice1);
                    order.setNumber(num);
                    order.setTotal_price(total_price);
                    int i = orderService.addOrder(order);
                    if (i > 0) {
                        System.out.println("添加成功");
                    }



                    break;

                case 3:
                    System.out.println("3.订单界面");
                    break;


                case 4:
                    System.out.println("4.修改密码");


                    break;
                case 5:
                    System.out.println("5.退出");
                    ViewMain viewMain = new ViewMain();
                    viewMain.main(null);
                    break;
            }
        }

    }
}

